package C;

import java.util.*;
import java.util.stream.Collectors;

public class C68 {

    static class App{
        String name;
        int weight;
        String start;
        String end;

        App(String input) {
            String[] records = input.split(" ");
            this.name = records[0];
            this.weight = Integer.parseInt(records[1]);
            this.start = records[2];
            this.end = records[3];
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        Map<App,Integer> map = new HashMap<>();
        for (int i=0;i<n;i++) {
            map.put(new App(sc.nextLine()),i);
        }
        String target = sc.nextLine();
        System.out.println(getResult(map,target));
    }
    //检验是否有公共时间,是否冲突
    public static boolean check(App app1, App app2) {
        if (app1.start.compareTo(app2.end)>0 || app1.end.compareTo(app2.start)<0)
            return false;
        return true;
    }

    public static String getResult(Map<App,Integer> map,String target) {
        List<Map.Entry<App,Integer>> list = map.entrySet()
                .stream()
                .sorted((e1,e2)->{
                     return e2.getKey().weight!=e1.getKey().weight ? e2.getKey().weight-e1.getKey().weight : e1.getValue() - e2.getValue();
                })
                .collect(Collectors.toList());
        List<App> res = new ArrayList<>();
        outer:
        for (int i = 0; i < list.size(); i++) {
            for (App app : res) {
                if (check(app,list.get(i).getKey())) {
                    continue outer;
                }
            }
            res.add(list.get(i).getKey());
        }

        for (int i = 0; i < res.size(); i++) {
            String start = res.get(i).start;
            String end = res.get(i).end;
            if (target.compareTo(start)>=0 && target.compareTo(end)<=0) {
                return res.get(i).name;
            }
        }
        return "NA";
    }
}
